package com.kye.express.business.order;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.kye.express.business.customer_code.bind_customer_code.BindCustomerCodeActivity;
import com.kye.express.business.name_certification.NameCertificationActivity;
import com.kye.express.business.order.add_order.AddOrderActivity;
import com.kye.model.SharedPreferencesConfig;
import com.kye.model.SharedPreferencesManager;
import com.kye.model.UserSession;
import com.kye.model.bean.user.User;

public class NameCertificationUtils {

    public static void checkStartActivity(Context context, Dialog customDialog, CheckListener checkListener) {
        final User user = UserSession.getInstance().getUser();
        if (!user.isLogin()) {
            checkListener.startLoginActivity();
        } else if (TextUtils.isEmpty(user.getCustomCode())) {
            if (customDialog == null) {
                customDialog = new MaterialDialog.Builder(context)
                        .title("提示")
                        .content("您未绑定客户编码，请先绑定或选择扫描二维码进行下单，如果没有客户编码或二维码，请联系您的市场专员获取")
                        .positiveText("绑定客户编码")
                        .neutralText("扫码下单")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                BindCustomerCodeActivity.startActivity(context);
                            }
                        })
                        .onNeutral(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                checkListener.checkNameCertification(CheckListener.Type.SCAN_ADD_ORDER);
                            }
                        })
                        .build();
            }

            checkListener.onCreateDialog(customDialog);

            if (!customDialog.isShowing()) customDialog.show();
        } else if (System.currentTimeMillis() - SharedPreferencesManager.get(SharedPreferencesConfig.KEY_NAME_CERTIFICATION, 0L) > 24 * 60 * 60 * 1000) {
            checkListener.checkNameCertification(CheckListener.Type.ADD_ORDER);
        } else {
            if (user.getCustomerType() == 1) {
                checkListener.checkCustomCodeForStartScanCodeAddOrder(user.getCustomCode());
            } else if (user.getCustomerType() == 0){
                AddOrderActivity.startActivity(context);
            }
        }
    }

    public static Dialog getValidationCertification(Context context, Dialog dialog, String content, CheckListener.Type type) {
        if (dialog != null) return dialog;

        dialog = new MaterialDialog.Builder(context)
                .title("实名认证")
                .content(content)
                .positiveText("立即认证")
                .negativeText("下次再说")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        NameCertificationActivity.startActivity(context);
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (type == NameCertificationUtils.CheckListener.Type.ADD_ORDER) {
                            AddOrderActivity.startActivity(context);
                        }
                    }
                })
                .build();

        return dialog;
    }

    public interface CheckListener {
        void onCreateDialog(Dialog dialog);

        void startLoginActivity();

        void checkNameCertification(Type type);

        void checkCustomCodeForStartScanCodeAddOrder(String customCode);

        enum Type {
            ADD_ORDER,
            SCAN_ADD_ORDER
        }
    }
}
